<?php
/**
 * @Author:      lyt
 * @DateTime:  2017-05-16 10:08:24
 * @Description: 用户充值记录
 */
class AcountUserRecharge extends Eloquent
{
    protected $table = "acount_user_recharge_record";

    public static $pay_sources = array(
        '1'=>'系统',
        '2'=>'智能pos'
    );
    //暂时充值记录的支付方式
    public static $paytype=array(
        "1"=>"线下支付现金",
        "2"=>"支付宝",
        "3"=>"微信",
        "7"=>"刷卡",
		'11'=>'线下支票',
		'12'=>'划拨',
		'13'=>'保险',
        '14'=>'调整充值',
        '20'=>'pos支付',
	);

    public static $pay_type_name = array(
        "1"=>"线下支付现金",
        "2"=>"支付宝",
        "3"=>"微信",
        "4"=>"大众点评",
        "5"=>"其他",
        "6"=>"线下支付挂单",
        "7"=>"刷卡",
        "8"=>"余额",
        "9"=>"复合",
        "10"=>"不记账结算",
        "49"=>'洗车卡',
        "69"=>'免费',
        "11"=>'线下支票',
        "12"=>'划拨充值',
        "13"=>'保险充值',
        "14"=>'调整充值',
        "15"=>'银行汇款',
        "16"=>'百度钱包',
        "17"=>'易付宝',
        "18"=>'京东钱包',
        "19"=>'QQ钱包',
        "20"=>'智能pos',
    );

    public static $recharge_type=array(
        "1"=>"订单消费",
        "2"=>"洗车单消费",
        '3'=>'撤销充值消费',
        '4'=>'退款消费',
        '5'=>'订单收回消费',
        '6'=>'余额调整消费',
        '7'=>'期初收回支出',
        '8'=>'套餐卡订单消费',
    );

    public static $rechargere_type=array(
        "1"=>"订单退款",
        "2"=>"洗车单退款",
        '3'=>'订单支付撤销退款',
        '4'=>'洗车单支付撤销退款',
    );


    public static $sc_paytype=array(
        "1"=>"线下支付现金",
        "2"=>"支付宝",
        "3"=>"微信",
        "7"=>"刷卡",
        '11'=>'线下支票',
        '20'=>'pos支付',
    );

    public static $sc_paytype_source=array(
        "1"=>array(1,2),
        '2'=>array(1,2),
        '3'=>array(1,2),
        "7"=>array(1,2),
        '11'=>array(1),        
        '20'=>array(2),
    );

    
    /**
     * @Author:      lyt
     * @DateTime:  2017-05-16 10:09:02
     * @Description: 根据门店 用户 获取 支出指定金额需要使用的 充值记录
     * 逻辑 优先使用 现金账户 在使用虚拟账户 优先使用本门店  
     * @param $shop_id 门店id
     * @param $user_id 用户ID
     * @param $amount 金额单位分
     * @param $acount_type 是否使用虚拟账户  2使用 其他不适用
     */
    public static function getUseRecharge($shop_id,$user_id,$amount,$acount_type=2,$sort="asc"){
        
        $finally_arr=array();        
        /**
        *step 1 本店现金账户
         */
        //获取 该用户在该门店的现金账户
        $account_no = Account::get_account_details_no($user_id,$shop_id,1);
        //获取 未使用完的支付记录
        $s_aur=AcountUserRecharge::where("account_no",$account_no)->where("status",1)->where("current_amount",">",0)->orderBy("implement_time",$sort)->get();
        foreach ($s_aur as $_s_aur) {
        	if($_s_aur->current_amount>=$amount){//说明这次充值记录满足需求
	    		$_s_aur->user_amount=$amount;
	    		$_s_aur->acount_type=1;
	    		$finally_arr[]=$_s_aur;
	    		return $finally_arr;
	    	}else{//说明 这次不满足需求
	    		$_s_aur->user_amount=$_s_aur->current_amount;
	    		$_s_aur->acount_type=1;
	    		$amount=$amount-$_s_aur->current_amount;
	    		$finally_arr[]=$_s_aur;
	    	}
        }        
        /**
        *step 2 其他门店现金账户
         */
        //获取该用户的其他门店的现金账户
        $account_no_arr=UserAccountDeatails::where('user_id',$user_id)->where('shop_id',"!=",$shop_id)->where('acount_type',1)->select("account_no")->get()->toArray();
         //获取 未使用完的支付记录
        $s_aur=AcountUserRecharge::whereIn("account_no",$account_no_arr)->where("status",1)->where("current_amount",">",0)->orderBy("implement_time",$sort)->get();
        foreach ($s_aur as $_s_aur) {
        	if($_s_aur->current_amount>=$amount){//说明这次充值记录满足需求
	    		$_s_aur->user_amount=$amount;
	    		$_s_aur->acount_type=1;
	    		$finally_arr[]=$_s_aur;
	    		return $finally_arr;
	    	}else{//说明 这次不满足需求
	    		$_s_aur->user_amount=$_s_aur->current_amount;
	    		$_s_aur->acount_type=1;
	    		$amount=$amount-$_s_aur->current_amount;
	    		$finally_arr[]=$_s_aur;
	    	}
        }
        if($acount_type==2){
            /**
            *step 3 本店虚拟账户
             */
            //获取 该用户在该门店的虚拟账户
            $account_no = Account::get_account_details_no($user_id,$shop_id,2);
            //获取 未使用完的支付记录
            $s_aur=AcountUserRecharge::where("account_no",$account_no)->where("status",1)->where("current_amount",">",0)->orderBy("implement_time",$sort)->get();
            foreach ($s_aur as $_s_aur) {
                if($_s_aur->current_amount>=$amount){//说明这次充值记录满足需求
                    $_s_aur->user_amount=$amount;
                    $_s_aur->acount_type=2;
                    $finally_arr[]=$_s_aur;
                    return $finally_arr;
                }else{//说明 这次不满足需求
                    $_s_aur->user_amount=$_s_aur->current_amount;
                    $_s_aur->acount_type=2;
                    $amount=$amount-$_s_aur->current_amount;
                    $finally_arr[]=$_s_aur;
                }
            }
            /**
            *step 4 其他门店虚拟账户
             */
            //获取该用户的其他门店的虚拟账户
            $account_no_arr=UserAccountDeatails::where('user_id',$user_id)->where('shop_id',"!=",$shop_id)->where('acount_type',2)->select("account_no")->get()->toArray();
             //获取 未使用完的支付记录
            $s_aur=AcountUserRecharge::whereIn("account_no",$account_no_arr)->where("status",1)->where("current_amount",">",0)->orderBy("implement_time",$sort)->get();
            foreach ($s_aur as $_s_aur) {
                if($_s_aur->current_amount>=$amount){//说明这次充值记录满足需求
                    $_s_aur->user_amount=$amount;
                    $_s_aur->acount_type=2;
                    $finally_arr[]=$_s_aur;
                    return $finally_arr;
                }else{//说明 这次不满足需求
                    $_s_aur->user_amount=$_s_aur->current_amount;
                    $_s_aur->acount_type=2;
                    $amount=$amount-$_s_aur->current_amount;
                    $finally_arr[]=$_s_aur;
                }
            }     
        }
           
        //如果都不满足说明数据支付过程中 修改  拒绝支付
        return array();
    }

     /**
     * @Author:      zy
     * @DateTime:  2017-07-12
     * @Description: 根据充值记录，获取当前的充值账户的信息
      * @param int $recharge_id 充值记录id
     */
    public static function RechargeInfo($Recharge_id){
        $info = AcountUserRecharge::from("acount_user_recharge_record as a")
                ->select("a.shop_id","b.acount_type","c.name")
                ->where("a.id",$Recharge_id)
                ->leftJoin("user_account_details as b","a.account_no","=","b.account_no")
                ->leftJoin("maintenance_shops as c","b.shop_id","=","c.id")
                ->get()->toArray();
        if (!empty($info)){
            return ['name'=>$info[0]['name'],'shop_id'=>$info[0]['shop_id'],'acount_type'=>$info[0]['acount_type']];
        }
        return "";
    }
    
    
}